<?php


namespace app\admin\controller;



use app\common\model\Board;
use app\common\model\Topnews;
use app\common\validate\Vld_board;
use app\common\validate\Vld_headline;
use think\exception\ValidateException;

class Headlines  extends Base
{
    public  function  get_detail()
    {//获取客户详细信息
        $id=1;
        $data = Topnews::field(' id,imgUrls1,imgUrls2,imgUrls3,jianjie_txt ')
            ->where('id',$id)
            ->find();
        //返回判断
        if ($data){
            $msg='OK';
            $status=200;
        }else{
            $msg='failed';
            $status=0;
        }
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>$data,
            'meta'=>$meta
        ];
        return json($res);
    }

    public  function  edit()
    {//修改信息
        $data=[
            'id'=>input('post.id'),
            'imgUrls1'=>input('post.imgUrls1'),
            'imgUrls2'=>input('post.imgUrls2'),
            'imgUrls3'=>input('post.imgUrls3'),
            'jianjie_txt'=>input('post.jianjie_txt')
        ];
        try
        {
            $validateres = validate(Vld_headline::class)
                ->scene('edit')
                ->check($data);
            if( $validateres)
            {//通过验证
                $mydata = Topnews::where('id',$data['id'])
                    ->find();
                $result= $mydata->save($data);
                if($result){
                    //判断添加是否成功
                    $msg="ok";
                    $status=200;
                }else{
                    $msg="fail";
                    $status=0;
                }
            }
        }
        catch (ValidateException  $e) {
            // 验证失败 输出错误信息
            $msg=  $e->getMessage();
            $status=0;
        }
        // 返回客户端信息
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>null,
            'meta'=>$meta
        ];
        return json($res);
    }
}
